﻿using System;

namespace HikvisionDemo
{
    class InitSdkUtils
    {
        public static bool Init()
        {
            // 初始化SDK
            bool result = false;

            result = CHCNetSDK.NET_DVR_Init();
            if (result)
            {
                Console.WriteLine($"SDK初始化成功");

                // 设置SDK日志路径
                CHCNetSDK.NET_DVR_SetLogToFile(3, "D:\\test\\SdkLog\\", true);

                // 设置异常消息回调方法
                bool bRet = CHCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, (IntPtr)0, new CHCNetSDK.EXCEPYIONCALLBACK(HandleException), new IntPtr());
            }
            else
            {
                uint iLastErr = CHCNetSDK.NET_DVR_GetLastError();
                Console.WriteLine($"SDK初始化失败，错误代码={iLastErr}");
            }

            return result;

        }

        public static void HandleException(uint dwType, int lUserID, int lHandle, IntPtr pUser)
        {
            // 全局异常处理
            switch (dwType)
            {
                case 0x8000:
                    Console.WriteLine("用户交互时异常（注册心跳超时，心跳间隔为2分钟）");
                    break;
                case 0x8002:
                    Console.WriteLine("报警异常");
                    break;
                case 0x8003:
                    Console.WriteLine("网络预览异常");
                    break;
                default:
                    Console.WriteLine("未处理异常");
                    break;
            }

        }
    }
}
